﻿using UnityEngine;
using System.Collections;

public class Enemy_1 : MonoBehaviour {

	private GameObject player;
	public float jump_force = 0f;
	public float speed = 80f;
	public float giveup_timer = 0f;
	public bool seeing_plr = false;
	public bool flying = false;
	private Vector3 moving_dir;
	private Vector3 waypoint;
	
	public Animator ani;
	
	public float lvl = 1f;

	// Use this for initialization
	void Start () {
		waypoint = transform.position;
		
		//starting_difficulty
	}
	
	// Update is called once per frame
	void Update () {
	
		//print ("close " + Vector3.Distance(transform.position,player.transform.position));
		//seeing_plr = false;
		
		if(/*renderer.isVisible ||*/ seeing_plr)
		{
			if(player==null)
				player = GameObject.FindGameObjectWithTag("Player");
			else
			{
				float dist = Vector3.Distance(transform.position,player.transform.position);
				if(dist < 25f || seeing_plr)
				{
					Vector3 dir = player.transform.position - transform.position;
					
					if(!flying)
						dir.y/=2;
					
					dir.Normalize();
					
					Vector3 raystart = transform.position + dir*collider2D.bounds.size.x*2.1f;
					
					RaycastHit2D hit = Physics2D.Raycast(raystart,dir,25f);
					
					if(hit.collider != null)
					{
						if(hit.collider.gameObject.tag == "Player")
						{
							seeing_plr = true;
							moving_dir = dir;
							giveup_timer = (float)Random.Range(20,40)/10;
						}
					}
					//if(seeing_plr == true)
					//	rigidbody2D.AddForce(new Vector2(moving_dir.x,moving_dir.y)*speed * Time.deltaTime);
					
					if(dist > 2f)
						rigidbody2D.AddForce(new Vector2(moving_dir.x,moving_dir.y)*speed * Time.deltaTime);
					else
						rigidbody2D.AddForce(new Vector2(moving_dir.x,moving_dir.y)*speed * Time.deltaTime*-1f);
				}
			}
		}
		
		giveup_timer -= Time.deltaTime;
		
		if(giveup_timer<0f)
		{
			seeing_plr = false;
		}
		if(seeing_plr == false)
			Patrol();
		
	}
	void Patrol()
	{
		//if(renderer.isVisible)
		{
			if(/*Vector3.Distance(transform.position,waypoint) < 0.1f || */giveup_timer<0f)
			{
				float rx = Random.Range(-100,100);
				float ry = Random.Range(-100,100);
				waypoint = transform.position + new Vector3(rx,ry)/20;
				giveup_timer = (float)Random.Range(0,20)/10;
			}
			else
			{
				Vector3 dir = waypoint - transform.position;
				
				if(!flying)
					dir.y=0f;
					
				dir.Normalize();
				
				rigidbody2D.AddForce(new Vector2(dir.x,dir.y/2)*speed * Time.deltaTime);
			}
		}
		
		Debug.DrawLine(transform.position,waypoint);
	}
	
	void OnCollisionEnter2D(Collision2D c)
	{
		if(flying)
		{
			waypoint = transform.position + (Vector3)c.contacts[0].normal;
		}
		else
		{
			if((c.contacts[0].normal.x < 0 && rigidbody2D.velocity.x > 0)||
			(c.contacts[0].normal.x > 0 && rigidbody2D.velocity.x < 0))
			{
				rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jump_force);
				if(ani)
				{
					ani.SetBool("jumping",true);
				}
			}
			else if(ani)
			{
				ani.SetBool("jumping",false);
			}
		}
	}
}
